package www.yingdi365.com.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseNotLeftActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.listener.HuanHangWatcher;
import www.yingdi365.com.model.AppLoginResult;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.ACache;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;

public class DengLuActivity extends ZjbBaseNotLeftActivity implements View.OnClickListener {

    private TextView textWangJiMM;
    private EditText editPhone;
    private EditText editPsw;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deng_lu);
        init();
    }

    @Override
    protected void initSP() {
        ACache aCache = ACache.get(DengLuActivity.this, Constant.ACACHE.ACCOUNT);
        account = aCache.getAsString(Constant.ACACHE.ACCOUNT);
    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void findID() {
        textWangJiMM = (TextView) findViewById(R.id.textWangJiMM);
        editPhone = (EditText) findViewById(R.id.editPhone);
        editPsw = (EditText) findViewById(R.id.editPsw);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("登录");
        textWangJiMM.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        editPhone.addTextChangedListener(new HuanHangWatcher(editPsw, 11));
        if (!TextUtils.isEmpty(account)) {
            editPhone.setText(account);
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.btnLogin).setOnClickListener(this);
        findViewById(R.id.imageBack).setOnClickListener(this);
        findViewById(R.id.textWangJiMM).setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textWangJiMM:
                Intent intent = new Intent();
                intent.setClass(this, WangJiMMActivity.class);
                startActivity(intent);
                break;
            case R.id.imageBack:
                finish();
                break;
            case R.id.btnLogin:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    Toast.makeText(DengLuActivity.this, "请填写登录账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editPsw.getText().toString().trim())) {
                    Toast.makeText(DengLuActivity.this, "请填写登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                showLoadingDialog();
                OkObject okObject = getOkObject();
                OkGo.post(okObject.getUrl())//
                        .tag(this)//
                        .upJson(okObject.getJson())//
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelLoadingDialog();
                                LogUtil.LogShitou("DengLuActivity--onSuccess", "登录返回" + s);
                                try {
                                    AppLoginResult appLoginResult = GsonUtils.parseJSON(s, AppLoginResult.class);
                                    if (appLoginResult.isSuccess()) {
                                        ACache aCache = ACache.get(DengLuActivity.this, Constant.ACACHE.ACCOUNT);
                                        aCache.put(Constant.ACACHE.ACCOUNT, editPhone.getText().toString().trim());
                                        ACache yingDi = ACache.get(DengLuActivity.this, Constant.ACACHE.YING_DI);
                                        yingDi.put(Constant.ACACHE.USER_INFO,appLoginResult);
                                        Intent intent = new Intent();
                                        intent.setClass(DengLuActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        Toast.makeText(DengLuActivity.this, appLoginResult.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(DengLuActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                cancelLoadingDialog();
                                Toast.makeText(DengLuActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.LOGINSERVICE + editPhone.getText().toString().trim() + "/login";
        HashMap<String, String> params = new HashMap<>();
        params.put("password", editPsw.getText().toString());
        return new OkObject(null,params, url);
    }
}
